package Exercise;

import java.util.Random;
public class money {
    public class randomredpacket{
        private int leftmoney;
        private int leftnum;
        private Random rnd;
        public randomredpacket(int total,int num){
            this.leftmoney=total;
            this.leftnum=num;
            this.rnd=new Random();
        }
        public synchronized int nextmoney(){
            if(this.leftnum<=0){
               System.out.println("wu");
            }
            if(this.leftnum==1){
                return this.leftmoney;
            }
            double max=this.leftmoney/this.leftnum*2d;
            int money=(int)(rnd.nextDouble()*max);
            money=Math.max(1, money);
            this.leftmoney-=money;
            this.leftnum--;
            return money;
        }
    }
    
}
